package com.thc.servlet;

import com.alibaba.fastjson.JSONObject;
import com.thc.dao.SheetDao;
import com.thc.domain.Sheet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SheetServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        String content = request.getParameter("content");
        SheetDao sheetDao = new SheetDao();
        Map<String, List<Sheet>> map= sheetDao.search( content);
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        response.setHeader("content-type", "text/html;charset=UTF-8");
        List<Sheet> list = map.get("res");
        Map<String,Object> res=new HashMap<>();
        res.put("list", list);
        res.put("size", "共"+list.size()+"条");
        String json= JSONObject.toJSONString(res);
        out.println(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
